import argparse
import pymysql
import os

# 写入到文件
# cursor
# values
# 批量读取ftpdata_path文件内容
def write_to_file(cursor, values, filename):
    ids = tuple(values.keys())
    sql = "SELECT id,phone FROM mobile_encrypt WHERE id IN %s"
    # 执行SQL语句，并将结果中的id、phone、date保存到文件中
    cursor.execute(sql, (ids,))
    results = cursor.fetchall()
    # basedir = /data/dev10086/phonedata/
    filePath = "/data/dev10086/phonedata/" + filename
    
    for result in results:
        id, phone = result
        # 获取values中id对应的date,并只获取月份
        month = values[id][4:6]
        with open(filePath, 'a') as f:
            f.write("{}|{}|{}\n".format(phone, month, id))


def main():
    parser = argparse.ArgumentParser()
    # parser.add_argument("--phone", help="Phone number")
    parser.add_argument("--type", help="the model type's sign, such as 'zaixin_model_hq' or 'zaixin_model_zx' or 'zaixin_model_zf'")
    args = parser.parse_args()
    
    type = args.type
    if type is None:
        print("please input the type")
        return
    
    ftpdataDir = "/data/dev10086/ftpdata/"
    if type == "zx":
        fileName = "zaixin_zhuangxiu_rsltout_20240201.txt"
        ftpdata_path = ftpdataDir + fileName
    elif type == "hq":
        fileName = "zaixin_hunqing_rsltout_20240201.txt"
        ftpdata_path = ftpdataDir + fileName
    elif type == "zf":
        fileName = "zaixin_zhifu_rsltout_20240201.txt"
        ftpdata_path = ftpdataDir + fileName
    else:
        print("please input the correct type")
        return
    
    targetPath = "/data/dev10086/phonedata/" + fileName
    last_line = ""
    if os.path.exists(targetPath):
        with open(targetPath, 'r') as f:  
            f.seek(-1, 2)  # 将文件指针移动到文件末尾  
            last_line = f.readline()  # 读取最后一行  
    
    # 创建数据库连接
    db = pymysql.connect(host='localhost', user='root', password='lcz19860109', db='dev10086', charset='utf8')
    # 创建游标对象
    cursor = db.cursor()
    batch_size = 1000  # 每次插入1000行
     
    if last_line:
        last_id = int(last_line.split('|')[1])
        last_id = "id_" + str(last_id)
        # 查找ftpdata_path文件中last_id的行号，从该行号开始读取
        with open(ftpdata_path, 'r') as file:  
            # 查找last_id的行号  
            for i, line in enumerate(file):  
                if last_id in line:  
                    start_line = i + 1  # 从下一行开始读取  
                    break  
                
            # 从start_line开始读取文件内容  
            with open(ftpdata_path, 'r') as file:  
                for i, line in enumerate(file):  
                    if i >= start_line:
                        values = {}
                        id, date = line.strip().split('|')
                        id = int(id.split('_')[1])
                        # values 添加 {id:date}
                        values[id] = date
                    
                        if len(values) >= batch_size:
                            # 查询对应的phone并保存到
                            write_to_file(cursor, values, fileName)    
                            values.clear()
                if values:
                    # insert_into_db(values)
                    write_to_file(cursor, values, fileName)
                    values.clear()
                            
        
    else :
        with open(ftpdata_path, 'r') as f:
            values = {}
        
            for line in f:
                id, date = line.strip().split('|')
                id = int(id.split('_')[1])
                # values 添加 {id:date}
                values[id] = date
                
                if len(values) >= batch_size:
                    # 查询对应的phone并保存到
                    write_to_file(cursor, values, fileName)    
                    values.clear()
                    
            if values:
                # insert_into_db(values)
                write_to_file(cursor, values, fileName)
                values.clear()
            
    # 关闭数据库连接
    db.close()
    # fileName 转换成phone文件完成，保存到/data/dev10086/phonedata/目录下的文件名为fileName
    print("fileName 转换成phone文件完成，保存到/data/dev10086/phonedata/目录下的文件名为fileName")
    
if __name__ == '__main__':
    main()